<?php

class RankController extends Zend_Controller_Action {

    public function init() {
        $ajaxContext = $this->_helper->getHelper("AjaxContext");
        $ajaxContext->addActionContext("getrank", "json");
        $ajaxContext->addActionContext("saveranksetting", "json");
        $ajaxContext->addActionContext("getranksetting", "json");
        $ajaxContext->initContext();
    }

    public function getrankAction() {
        $pid = $this->getRequest()->getParam("pid");
        $rankPrice = new Application_Model_DbTable_RankingPrice();
        $rank = array(
            'ERGONOMICS' => round(($rankPrice->getRank4ED('ERGONOMICS', $pid)) / 100,1),
            'DURABILITY' => round(($rankPrice->getRank4ED('DURABILITY', $pid)) / 100,1),
            'PRACTICAL' => round(($rankPrice->getPraticalRank($pid)) / 100,1),
            'WEIGHT' => round(($rankPrice->getWeightRank($pid)),1) ,
            'PRICE' => round(($rankPrice->getPriceRank($pid)),1)
        );
        $this->_helper->getHelper('Json')->sendJson($rank);
    }

    public function saveranksettingAction() {   
        $json = $this->getRequest()->getParam("rankset");
        //$json = '{"RankRule":[{"ID":1,"Rule":50,"SubcatRule":50}],"WeightPrice":[{"ID":1,"MinWeight":15,"MaxWeight":20,"MinPrice":10,"MaxPrice":12}]}';
        $jsonArray = Zend_Json::decode($json);
        $rules = $jsonArray['RankRule'];
        $rankRule = new Application_Model_DbTable_RankingRule();
        if (count($rules) > 0) {
            foreach ($rules as $rule) {                
                $data = array(
                    'Rule' => $rule['Rule'],
                    'SubcatRule' => $rule['SubcatRule']
                );
                $where = $rankRule->getAdapter()->quoteInto("ID = ? ", $rule['ID']);
                $rankRule->update($data, $where);
            }
        }
        $wpArray = $jsonArray['WeightPrice'];
        $rankWeight = new Application_Model_DbTable_RankingWeight();
        $rankPrice = new Application_Model_DbTable_RankingPrice();
        if (count($wpArray) > 0) {
            foreach ($wpArray as $wpa) {
                $where = $rankWeight->getAdapter()->quoteInto("TypeProductID = ? ", $wpa['ID']);
                $weightData = array(
                    'MinWeight' => $wpa['MinWeight'],
                    'MaxWeight' => $wpa['MaxWeight']
                );
                $rankWeight->update($weightData,$where);
                $priceData = array(
                    'MinPrice' => $wpa['MinPrice'],
                    'MaxPrice' => $wpa['MaxPrice']
                );
                $rankPrice->update($priceData,$where);
            }
        }

        $this->_helper->getHelper('Json')->sendJson(array('message' => "Save Successfully"));
    }

    public function getranksettingAction() {
        $rankSetting = array();
        $rankRule = new Application_Model_DbTable_RankingRule();
        $sql = "SELECT r.ID,r.MainCat,r.Subcat,s.Name, r.SubcatRule,r.Rule FROM RankingRule r JOIN SupplyType s ON s.ID = r.SupplyType";
        $rules = $rankRule->getAdapter()->fetchAssoc($sql);
        $wp = "select t.TypeProduct,w.MinWeight,w.MaxWeight,p.MinPrice,p.MaxPrice from RankingWeight w,RankingPrice p,TypeProduct t where t.ID=w.TypeProductID and t.ID=p.TypeProductID and t.ID in(3,4)";
        $rankPriceWeight = $rankRule->getAdapter()->fetchAssoc($wp);
        $rankSetting['RankingRule'] = array_values($rules);
        $rankSetting['WeightPrice'] = $rankPriceWeight;
        $this->_helper->getHelper('Json')->sendJson($rankSetting);
    }

}
